package com.g2room.myuall.grap.univ;

import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.g2room.myuall.grap.univ.to.MajorRankTo;
import com.g2room.myuall.grap.univ.to.UnivRankTo;
import com.g2room.myuall.util.DbUtil;
import com.g2room.myuall.util.StringUtil;

public class GrapRank {
	// http://www.gaokaopai.com/paihang-otype-8.html
	// http://gaokao.xdf.cn/201601/10402914.html
	// http://www.gaokao.com/z2015/dxxyph/#
	// http://www.dxsbb.com/news/list_75.html
	static Map<String, String> provUrl = new HashMap<String, String>();
	public static void main(String[] args) {
		String url = "http://www.gaokaopai.com/rank-index.html";
//		getList(url);
		// 1、录取难度；2、就业排行；3、知名度排行；4、专业就业排行；5、专业薪酬排行。
//		getUnivRank("就业排行", "http://www.gaokaopai.com/paihang-otype-5.html", 2);
		getUnivRank("知名度排行", "http://www.gaokaopai.com/paihang-otype-6.html", 23);
		getMajorRank("http://www.gaokaopai.com/paihang-otype-7.html", 1);
		getMajorRank("http://www.gaokaopai.com/paihang-otype-8.html", 2);
	}
	public static void getList(String url, int type) {
		Document doc = null;
		try {
			doc = new GenericMyuall().getDoc(url);
		} catch (Exception e1) {
			e1.printStackTrace();
			System.out.println("err*******="+ url);
			return;
		}
		Elements els = doc.getElementsByAttributeValue("class", "link").first().getElementsByTag("a");
		for(Element el : els) {
			provUrl.put(el.text(), el.attr("href"));
		}
		for(String prov : provUrl.keySet()) {
			System.out.println(prov + "==" + provUrl.get(prov));
			getUnivRank(prov, provUrl.get(prov), type);
		}
	}
	
	public static void getUnivRank(String prov, String url, int type) {
		String body = null;
		for(int i = 0; i<10000; i=i + 25) {
			try {
				body = new GenericMyuall().getJsonContentByCookie(url, (i==0?1:i)+"");
			} catch (Exception e1) {
				e1.printStackTrace();
				System.out.println("err*******="+ url);
				insert2ErrDb(url, prov, 4);
				return;
			}
			if(body.toString().contains("safehard")) {
				body = body.substring(body.indexOf("["), body.indexOf("]")+1);
				System.out.println(body.toString());
				List<UnivRankTo> urts = JSON.parseArray(body, UnivRankTo.class);
				insert2Db(urts, prov, type);
			} else {
				System.out.println("============"+body.toString());
				break;
			}
		}
	}
	
	public static void insert2Db(List<UnivRankTo> urts, String prov, int type) {
		try {
			PreparedStatement ps = DbUtil.getConn().prepareStatement("insert into univ_rank(property,matriculation,rank_prov_nam,province,batch,schoolname,rank,schoolid,type,jiuye_level) values(?,?,?,?,?,?,?,?,?,?)");
			for(UnivRankTo ur : urts) {
				ps.setString(1, ur.getUni_type());
				ps.setDouble(2, StringUtil.isEmpty(ur.getSafehard())?0:Double.valueOf(ur.getSafehard()));
				ps.setString(3, prov);
				ps.setString(4, ur.getCity_code());
				ps.setString(5, "本科一批");
				ps.setString(6, ur.getUni_name());
//				ps.setInt(7, StringUtil.isEmpty(ur.getJiuye_rank())?0:Integer.valueOf(ur.getJiuye_rank()));
				ps.setInt(7, StringUtil.isEmpty(ur.getAware_rank())?0:Integer.valueOf(ur.getAware_rank()));
				ps.setInt(8, StringUtil.isEmpty(ur.getUni_id())?0:Integer.valueOf(ur.getUni_id()));
				ps.setInt(9, type);
				ps.setString(10, ur.getAware_level());
//				ps.setString(10, ur.getJiuye_level());
				// 1、录取难度；2、就业排行；3、知名度排行；4、专业就业排行；5、专业薪酬排行。
				// property,matriculation,rank_prov_nam,province,batch,schoolname,rank,schoolid
//				private String uni_name; //" : "\u4e1c\u5357\u5927\u5b66",
//				private String uni_id;
//				private String city_code;
//				private String uni_type;//" : "\u7efc\u5408\u7c7b",
//				private String rank;//" : "26",
//				private String safehard;//" : "90.98"
				ps.addBatch();
			}
			ps.executeBatch();
			
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	public static void getMajorRank(String url, int type) {
		String body = null;
		for(int i = 0; i<10000; i=i + 25) {
			try {
				body = new GenericMyuall().getJsonContentByCookie(url, (i==0?1:i)+"");
			} catch (Exception e1) {
				e1.printStackTrace();
				System.out.println("err*******="+ url);
				insert2ErrDb(url, "", type);
				return;
			}
			if(body.toString().contains("safehard")) {
				body = body.substring(body.indexOf("["), body.indexOf("]")+1);
				System.out.println(body.toString());
				List<MajorRankTo> urts = JSON.parseArray(body, MajorRankTo.class);
				insert2MajorDb(urts, type);
			} else {
				System.out.println("============"+body.toString());
				break;
			}
		}
	}
	
	public static void insert2MajorDb(List<MajorRankTo> urts, int type) {
		try {
			PreparedStatement ps = DbUtil.getConn().prepareStatement("insert into major_rank(major_name,level,content,main_subjects,major_code,type) values(?,?,?,?,?,?)");
			for(MajorRankTo ur : urts) {
				// major_name,level,salary,main_subjects,major_code
				ps.setString(1, ur.getMajor_name());
				ps.setString(2, ur.getType());
				ps.setString(3, StringUtil.isEmpty(ur.getSalary_avg()) ? ur.getEmploy_range() : ur.getSalary_avg());
				ps.setString(4, ur.getMain_subjects());
				ps.setString(5, ur.getMajor_code());
				ps.setInt(6, type);
				ps.addBatch();
			}
			ps.executeBatch();
			
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void insert2ErrDb(String url, String prov, int type) {
		try {
			PreparedStatement grapPs = DbUtil.getConn().prepareStatement("INSERT INTO `univ_err` (`url`,type, prov) "
					+ "values(?,?,?)");
			grapPs.setString(1, url);
			grapPs.setInt(2, type);
			grapPs.setString(3, prov);
			grapPs.execute();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
}
